package edu.luc.cs.trull.demo.task;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

/**
 * A user interface for controlling and monitoring progress of a Trull task. 
 */
public class ControlPanel extends JPanel {

	private BorderLayout borderLayout1 = new BorderLayout();
  private JProgressBar progressBar = new JProgressBar();
  private JPanel jPanel1 = new JPanel();
  private JButton jButton1 = new JButton();
  private JButton jButton2 = new JButton();
  private JButton jButton3 = new JButton();
  private JButton jButton4 = new JButton();
  private GridLayout gridLayout1 = new GridLayout();

  private ArrayList listeners = new ArrayList();

  public ControlPanel() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  private void jbInit() throws Exception {
    this.setLayout(borderLayout1);
    jButton1.setActionCommand("Start");
    jButton1.setText("Start");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton1_actionPerformed(e);
      }
    });
    jButton2.setActionCommand("Suspend");
    jButton2.setText("Suspend");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton2_actionPerformed(e);
      }
    });
    jButton3.setActionCommand("Resume");
    jButton3.setText("Resume");
    jButton3.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton3_actionPerformed(e);
      }
    });
    jButton4.setActionCommand("Stop");
    jButton4.setText("Stop");
    jButton4.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton4_actionPerformed(e);
      }
    });
    jPanel1.setLayout(gridLayout1);
    this.add(progressBar,  BorderLayout.SOUTH);
    this.add(jPanel1,  BorderLayout.CENTER);
    jPanel1.add(jButton1, null);
    jPanel1.add(jButton2, null);
    jPanel1.add(jButton3, null);
    jPanel1.add(jButton4, null);
  }

  void jButton1_actionPerformed(ActionEvent e) {
    fireAction(e);
  }

  void jButton2_actionPerformed(ActionEvent e) {
    fireAction(e);
  }

  void jButton3_actionPerformed(ActionEvent e) {
    fireAction(e);
  }

  void jButton4_actionPerformed(ActionEvent e) {
    fireAction(e);
  }

  public JProgressBar getProgressBar() {
    return progressBar;
  }

  public synchronized void addActionListener(ActionListener l) {
    listeners.add(l);
  }

  public synchronized void removeActionListener(ActionListener l) {
    listeners.remove(l);
  }

  public void fireAction(ActionEvent event) {
    ArrayList copyOfListeners = null;
    synchronized (this) {
      copyOfListeners = (ArrayList) listeners.clone();
    }
    Iterator i = copyOfListeners.iterator();
    while (i.hasNext()) {
      ((ActionListener) i.next()).actionPerformed(event);
    }
  }
}